package com.sskj.lib.util;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.util.DisplayMetrics;

import com.sskj.common.base.App;
import com.sskj.common.util.SPUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * 作者 :吕志豪
 * 简书：https://www.jianshu.com/u/6e525b929aac
 * github：https://github.com/lvzhihao100
 * 描述：
 * 创建时间：2018-10-08 16:29
 */
public class LanguageUtil {
    private static final String KEY_APP_LANGUAGE = "language";
    private static final String LANGUAGE = "language";
    public static List<Locale> locales = null;

    static {
        locales = new ArrayList<>();
        locales.add(Locale.ENGLISH);
        locales.add(Locale.CHINESE);
        locales.add(Locale.JAPANESE);
        locales.add(Locale.KOREAN);
    }

    public static void setLocales(List<Locale> locales) {
        com.sskj.common.util.LanguageUtil.locales = locales;
    }

    //更改App语言
    public static void changeAppLanguage(Context context, Locale locale, boolean persistence) {
        if (getAppLocale(context) != locale) {
            Configuration configuration = context.getResources().getConfiguration();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                configuration.setLocale(locale);
            } else {
                configuration.locale = locale;
            }
            DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
            context.getResources().updateConfiguration(configuration, displayMetrics);
            if (persistence) {
                saveLanguageSetting(context, locale);
            }
        }
//        Resources resources = context.getApplicationContext().getResources();
//        DisplayMetrics metrics = resources.getDisplayMetrics();
//        Configuration configuration = resources.getConfiguration();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
//            configuration.setLocale(locale);
//        } else {
//            configuration.locale = locale;
//        }
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//            LocaleList localeList = new LocaleList(locale);
//            LocaleList.setDefault(localeList);
//            configuration.setLocales(localeList);
//            context.getApplicationContext().createConfigurationContext(configuration);
//            context.createConfigurationContext(configuration);
//            Locale.setDefault(locale);
//        }
//
//        resources.updateConfiguration(configuration, metrics);
//
//        if (persistence) {
//            saveLanguageSetting(context, locale);
//        }
    }

    public static Context updateResources(Context context, Locale locale) {
        Locale.setDefault(locale);
        Resources res = context.getResources();
        Configuration config = new Configuration(res.getConfiguration());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale);
            context = context.createConfigurationContext(config);
        } else {
            config.locale = locale;
            res.updateConfiguration(config, res.getDisplayMetrics());
        }
        return context;
    }


    public static boolean isSameWithSetting(Context context) {
        String lang = context.getResources().getConfiguration().locale.getLanguage();
        return lang.equals(getAppLanguage(context));
    }
    //App 语言持久化

    public static void saveLanguageSetting(Context context, Locale locale) {

        SPUtil.put(KEY_APP_LANGUAGE, locale.getLanguage(), LANGUAGE);

    }

    public static String getAppLanguage(Context context) {
        return (String) SPUtil.get(KEY_APP_LANGUAGE, Locale.getDefault().getLanguage(), LANGUAGE);
    }

    public static Locale getAppLocaleByLanguage(String language) {
        for (Locale locale : locales) {
            if (locale.getLanguage().equals(language))
                return locale;
        }
        return Locale.ENGLISH;
    }

    public static Locale getAppLocaleByName(String name) {
        for (Locale locale : locales) {
            if (locale.getDisplayName().equals(name))
                return locale;
        }
        return Locale.ENGLISH;
    }

    public static boolean isChinese() {
        return getAppLocale(App.INSTANCE) == Locale.CHINESE;
    }

    public static boolean isSimpleChinese() {
        return getAppLocale(App.INSTANCE) == Locale.SIMPLIFIED_CHINESE;
    }

    public static Locale getAppLocale(Context context) {
        String lang = getAppLanguage(context);
        return getAppLocaleByLanguage(lang);
    }
}
